﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using saigonbook.Biz;
using System.Text.RegularExpressions;

namespace hethongbansach.Controllers
{
    public class sachController : Controller
    {
        public List<BIZ_sach> danhsachsach = new List<BIZ_sach>();
        public List<BIZ_sach> danhsachconhang = new List<BIZ_sach>();
        int pagesize = 6;

        public void dssach()
        {
            danhsachsach.Clear();
            danhsachsach = BIZ_sach.Select_sach();
        }

        private int soluongcon(int ma)
        {
            BIZ_tonkho tk = new BIZ_tonkho();
            tk.ngay = DateTime.Now.Date;
            tk.sach = new BIZ_sach();
            tk.sach.masach = ma;
            return tk.soluongtonkho(tk);
        }

        public void dsconhang()
        {
            danhsachconhang = BIZ_sach.Select_sach();
        //    danhsachconhang = (from s in danhsachconhang
        //                       where soluongcon(s.masach) > 0
        //                       select s).ToList();
        }

        //
        // GET: /sach/

        [ChildActionOnly]
        public ActionResult KhungTimKiem()
        {
            return PartialView();
        }

        [ChildActionOnly]
        public ActionResult sachbanchay()
        {
            dsconhang();
            List<BIZ_sach> sachbanchay = new List<BIZ_sach>();

            sachbanchay = (from s in danhsachconhang
                           orderby s.giabia descending
                           select s).Take(6).ToList();
            return PartialView(sachbanchay);
        }

        //Chi tiết sản phẩm
        public ActionResult chitietsanpham()
        {
            dssach();
            BIZ_sach chitietsanpham = new BIZ_sach();
            int masach = int.Parse(Request.QueryString["masach"].ToString());
            int soluongton = 0;
            foreach (BIZ_sach item in danhsachsach)
            {
                if (item.masach == masach)
                {
                    chitietsanpham = item;
                    break;
                }
            }
            BIZ_tonkho tk = new BIZ_tonkho();
            tk.ngay = DateTime.Now.Date;
            tk.sach = new BIZ_sach();
            tk.sach.masach = masach;
            soluongton = tk.soluongtonkho(tk);
            ViewBag.soluongcon = soluongton;
            if (chitietsanpham == null) return HttpNotFound();
            return View(chitietsanpham);
        }

        //Sản phẩm cùng loại
        [ChildActionOnly]
        public ActionResult sanphamcungloai(int page = 1)
        {
            dsconhang();
            int masach = int.Parse(Request.QueryString["masach"].ToString());
            List<BIZ_sach> sanphamcungloai = new List<BIZ_sach>();
            int loai = 0;
            foreach (BIZ_sach item in danhsachconhang)
            {
                if (item.masach == masach)
                    loai = (int)item.loai.maloai;
            }

            foreach (BIZ_sach item in danhsachconhang)
            {
                if (item.loai.maloai == loai && item.masach != masach)
                    sanphamcungloai.Add(item);
            }
            int _pagesize = 3;
            ViewBag.masp = masach;
            ViewBag.TotalPages = Math.Ceiling((double)sanphamcungloai.Count / _pagesize);
            return PartialView(sanphamcungloai.Skip((page - 1) * _pagesize).Take(_pagesize));
        }

        public ActionResult DanhMuc(int page = 1)
        {
            dsconhang();
            string tenloai = "";
            int maloai = int.Parse(Request.QueryString["ma"].ToString());
            List<BIZ_sach> sanpham = new List<BIZ_sach>();
            foreach (BIZ_sach item in danhsachconhang)
                if (item.loai.maloai == maloai)
                {
                    sanpham.Add(item);
                    tenloai = item.loai.tenloai;
                }
            ViewBag.TotalPages = Math.Ceiling((double)sanpham.Count / pagesize);
            ViewBag.maloai = maloai;
            ViewBag.tenloai = tenloai;
            return View(sanpham.Skip((page - 1) * pagesize).Take(pagesize));
        }

        
         public ActionResult TimKiem(int page=1)
        {
            dsconhang();
            string id = Request.QueryString["chuoitk"];
            id = id.ToLower();
            List<BIZ_sach> kqtimkiem = new List<BIZ_sach>();
            foreach (BIZ_sach item in danhsachconhang)
            {
                if (item.tensach.ToLower().Contains(id))
                    kqtimkiem.Add(item);
            }
            ViewBag.Chuoi = id;
            ViewBag.KhongTimThay = "Không tìm thấy";
            ViewBag.TotalPages = Math.Ceiling((double)kqtimkiem.Count / pagesize);
            return View(kqtimkiem.Skip((page - 1) * pagesize).Take(pagesize));
        }

        public bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }
        
        public ActionResult TimKiemNangCao(int page = 1)
        {
            dsconhang();
            string loaihang = Request.QueryString["loaisach"];
            string tensp = Request.QueryString["tensach"];
            tensp = tensp.ToLower();
            string giatu = Request.QueryString["giatu"];
            string den = Request.QueryString["den"];
            int _giatu = 0;
            int _giaden = 0;
            if (loaihang.Equals("tatca"))
            {
                loaihang = "";
            }
            if (tensp.Equals("tên sách"))
            {
                tensp = "";
            }
            if (IsNumber(giatu))
            {
                _giatu = int.Parse(giatu);
            }
            else
                _giatu = 0;

            if (IsNumber(den))
            {
                _giaden = int.Parse(den);
            }
            else
                _giaden = 30000000;
            List<BIZ_sach> kqtimkiem = danhsachconhang;
            if (!String.IsNullOrEmpty(loaihang))
            {
                kqtimkiem = kqtimkiem.Where(t => t.loai.maloai.ToString() == loaihang).ToList();
            }
            if (!String.IsNullOrEmpty(tensp))
            {
                kqtimkiem = kqtimkiem.Where(t => t.tensach.ToString().ToLower().Contains(tensp)).ToList();
            }
            kqtimkiem = kqtimkiem.Where(t => (t.giaban >= _giatu && t.giabia <= _giaden)).ToList();
            ViewBag.KhongTimThay = "Không tìm thấy các sản phẩm thỏa điều kiện";
            ViewBag.loaihang = loaihang;
            ViewBag.tensp = tensp;
            ViewBag.giatu = giatu;
            ViewBag.giaden = den;
            ViewBag.TotalPages = Math.Ceiling((double)kqtimkiem.Count / pagesize);
            return View(kqtimkiem.Skip((page - 1) * pagesize).Take(pagesize));
        }
    }
}
